// pages/member/address/address.js
const app = getApp()
import {
  list
} from '../../../api/address'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: [],
    type: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type == "product") {
      this.data.type = options.type
    }else if(options.type=='change'){
      this.data.type = options.type
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.addressListRequest()
  },

  

  // 获取收货地址列表的请求
  addressListRequest() {
//     console.log("口令--" + app.globalData.token)
    let addressInfo = []
    list().then(
      res => {
        // console.log("收货地址列表的数据--" + JSON.stringify(res))
        if (res.type == "success") {
          if (res.data.data.length > 0) {
            res.data.data.map((item) => {
              let addressInfoItem = {}
              addressInfoItem.id = item.id
              addressInfoItem.address = item.address
              addressInfoItem.areaName = item.areaName
              addressInfoItem.building = item.building
              addressInfoItem.roomNumber = item.roomNumber
              addressInfoItem.isDefault = item.isDefault
              addressInfoItem.phone = item.phone
              addressInfoItem.level=item.level
              addressInfoItem.lat = item.lat
              addressInfoItem.lng = item.lng
              addressInfoItem.memberId = item.memberId
              addressInfoItem.consignee = item.consignee
              addressInfoItem.addressDetail = item.address
              addressInfo.push(addressInfoItem)
            })
          }
          this.setData({
            addressInfo: addressInfo
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },

  //编辑收货地址的事件
  editAddress: function(e) {
    console.info("e" + JSON.stringify(e))
    let id = e.currentTarget.dataset.id
    let addressDetail = {}
    if (this.data.addressInfo.length > 0) {
      this.data.addressInfo.map((item) => {
        console.info("item--"+JSON.stringify(item))
        if (item.id == id) {
          addressDetail = item
        }
      })
    }

    wx.navigateTo({
            url: '/pages/member/addressEdit/addressEdit?type=edit',
      success: function(res) {
        res.eventChannel.emit('sendAddressDetail', {
          type: "edit",
          data: addressDetail
        })
      },
    })
  },

  //添加收货地址的事件
  addAddress: function(e) {
    console.info("开始添加地址")
        wx.navigateTo({
                url: '/pages/member/addressEdit/addressEdit?type=add',
      success: function(res) {
        res.eventChannel.emit('sendAddressDetail', {
          type: "add"
        })
      },
    })
  },

  //订单确认时点击获取收货地址信息
  addressClick(data) {
    console.log(data)
    var _this = this
    if (this.data.type == "product") {
      // wx.setStorage({
      //   key: "product",
      //   data: data.currentTarget.dataset.item,
      //   success() {
      //     wx.navigateBack({
      //       delta: 1
      //     })
      //   },
      //   fail() {
      //     wx.showToast({
        //     icon: "none",
      //       title: '请稍后重试',
      //     })
      //   }
      // })
      wx.navigateBack({
        delta: 1
      })
      const eventChannel = _this.getOpenerEventChannel()
      eventChannel.emit('acceptDataFromOpenedPage', {
        data: data.currentTarget.dataset.item
      });
    }else if(this.data.type == "change"){
      app.globalData.lng=data.currentTarget.dataset.item.lng
      app.globalData.lat=data.currentTarget.dataset.item.lat
      app.globalData.address=data.currentTarget.dataset.item.address
      app.globalData.areaId=data.currentTarget.dataset.item.areaId
      app.globalData.areaName=data.currentTarget.dataset.item.areaName
      wx.navigateBack({
        delta: 1,
      })
    
    }
  }
})